package com.wwwzh.himalayafm.fragment.disfragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.activity.XmlyActivity;
import com.wwwzh.himalayafm.entity.entity_category.CategoryEntity;
import com.wwwzh.himalayafm.entity.entity_category.TotalEntity;
import com.wwwzh.himalayafm.retrofit.HttpService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.R.attr.width;

/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryDetailFragment extends Fragment implements View.OnClickListener {

    private XmlyActivity activity;
    private DrawerLayout mDrawerLayout;
    private HttpService mService;
    private GridLayout mGridLayout;
    private int width;
    private int height;
    private String title;
    private int cnt = 1;
    private ImageView mIvSpread;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (XmlyActivity) getActivity();
        mDrawerLayout = activity.getDrawerLayout();
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        // 屏幕宽度（像素）
        width = metric.widthPixels;
        height = metric.heightPixels / 2;//一般的屏幕高度
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        title = arguments.getString("title");
        View view = inflater.inflate(R.layout.fragment_category_detail, container, false);
        TextView mTextView = (TextView) view.findViewById(R.id.tv_category_content_id);
        mTextView.setText(title);
        mGridLayout = (GridLayout) view.findViewById(R.id.gl_category_detail_id);
        mGridLayout.setOrientation(GridLayout.HORIZONTAL);
        aboutNetWorkDown();
        return view;
    }

    private void aboutNetWorkDown() {
        mService = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        mService.getCategoryEntity("and-a1", "android", 13, 2)
                .enqueue(new Callback<TotalEntity>() {
                    @Override
                    public void onResponse(Call<TotalEntity> call, Response<TotalEntity> response) {
                        TotalEntity body = response.body();
                        List<CategoryEntity> list = body.getList();
                        for (int i = 1; i < list.size(); i++) {
                            final String perTitle = list.get(i).getTitle();
                            TextView textView = new TextView(getContext());
                            textView.setGravity(Gravity.CENTER);
                            textView.setWidth(100);
                            textView.setText(perTitle);
                            textView.setWidth(width / 4);
                            textView.setMarqueeRepeatLimit(5);
                            textView.setHeight(height / (list.size() / 4));
                            if (title.equals(perTitle)) {
                                textView.setTextColor(Color.RED);
                            }
                            textView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mDrawerLayout.closeDrawer(Gravity.RIGHT,false);

                                    CategoryDetailFragment detailFragment = new CategoryDetailFragment();
                                    Bundle args = new Bundle();
                                    args.putString("title", perTitle);
                                    detailFragment.setArguments(args);
                                    FragmentManager manager = activity.getSupportFragmentManager();
                                    FragmentTransaction transaction = manager.beginTransaction();
                                    transaction.replace(R.id.drawer_container, detailFragment).commit();
                                    mDrawerLayout.openDrawer(Gravity.RIGHT, true);
                                }
                            });
                            mGridLayout.addView(textView);
                        }
                    }

                    @Override
                    public void onFailure(Call<TotalEntity> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initToolbar(view);
    }

    private void initToolbar(View view) {
        Toolbar mToolbar = (Toolbar) view.findViewById(R.id.tb_category_content_id);
        activity.setSupportActionBar(mToolbar);
        //获取ToolBar对应的ActionBar
        android.support.v7.app.ActionBar actionBar = activity.getSupportActionBar();
        //添加返回按钮
//        actionBar.setDisplayHomeAsUpEnabled(false);
        //是否展示标题
        actionBar.setDisplayShowTitleEnabled(false);
        //是否展示图标
//      actionBar.setDisplayShowHomeEnabled(false);
        ImageView mIvBack = (ImageView) mToolbar.findViewById(R.id.iv_category_back_id);
        mIvBack.setOnClickListener(this);
        mIvSpread = (ImageView) mToolbar.findViewById(R.id.iv_category_spread_id);
        mIvSpread.setOnClickListener(this);
        ImageView mIvSearch = (ImageView) mToolbar.findViewById(R.id.iv_category_search_id);
        mIvSearch.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_category_back_id:
                mDrawerLayout.closeDrawer(Gravity.RIGHT);
                break;
            case R.id.iv_category_spread_id:
                if (cnt % 2 != 0) {
                    mGridLayout.setVisibility(View.VISIBLE);
                    mIvSpread.setImageResource(R.mipmap.ic_triangle_up);
                } else {
                    mGridLayout.setVisibility(View.GONE);
                    mIvSpread.setImageResource(R.mipmap.ic_triangle_down);
                }
                cnt++;
                break;
            case R.id.iv_category_search_id:

                FragmentManager manager = activity.getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.drawer_container, new CategorySerachFragment()).commit();
                mDrawerLayout.openDrawer(Gravity.RIGHT, true);
                break;
        }
    }

    @Override
    public void onStop() {
        cnt = 1;
        super.onStop();
    }
}
